package com.cdeledu.thread2.c3concurrent;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CountDownLatchDemo implements Runnable {

	static final CountDownLatch end = new CountDownLatch(10);
	static final CountDownLatchDemo demo = new CountDownLatchDemo();

	@Override
	public void run() {
		//模拟检查任务
		try{
			Thread.sleep(new Random().nextInt(10) * 1000);
			System.out.println("check complete");
			end.countDown();
		}catch(InterruptedException e){
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws InterruptedException {
		ExecutorService exec = Executors.newFixedThreadPool(10);
		for(int i=0; i<10; i++){
			exec.submit(demo);
		}
		//等待检查结束
		//使用await方法，要求主线程等待所有10个检查任务全部完成。等10个任务全部完成后，主线程才能继续执行。
		end.await();
		System.out.println("Fire!");
		exec.shutdown();
	}

}
